import React, { Component } from 'react';
import {
	StyleSheet, Text, View, Image, TouchableOpacity, ScrollView, PixelRatio
} from "react-native";
import { GLOBAL } from '../../../../config/global';
import { style } from '../../../../theme/style';
import { Common } from '../../../../component/common/Common';
import { orderStyle, commonStyle, commen } from '../_common/style';
import OrderDetailsService from '../_common/service';

const scale = GLOBAL.SCALE;

export class DefiniteDetailsPage extends Component {
	static navigationOptions = ({ navigation }) => {
		let { orderType } = navigation.state.params || {}
		return {
			headerTitle: (orderType == 'P' ? '采购' : '销售') + '订单-' + (orderType == 'P' ? '收货' : '发货') + '明细',
			headerTintColor: '#272727',
			headerLeft: <Common type="headLeft" onPress={() => { navigation.goBack() }} color='#272727' />,
			headerRight: (<Image style={{ marginLeft: 18 * scale, width: 9 * scale, height: 16 * scale }} source={require('../../../../imgs/goBackEmpty.png')} />),
			headerTitleStyle: {
				flex: 1,
				textAlign: 'center'
			},
			headerStyle: {
				elevation: 0,
				backgroundColor: '#F5F5F5',
				borderBottomWidth: 0,
			},
		}
	};

	constructor(props) {
		super(props);
		this.sendData = this.props.navigation.state.params || {};
		console.log('this.sendData', this.sendData)
		this.state = {
			isVisible: false
		}
	}

	async	componentWillMount () {
		this._init();
	}

	componentWillUnmount () {

	}

	//初始调用
	_init = () => {
		this._getData();
	}

	//获取详情数据
	_getData = async () => {
		let rsData = await OrderDetailsService.stockMxDetail(this.sendData);
		console.log('明细数据详情', rsData);
		if (rsData) {
			this.setState({ data: rsData })
		} else {
			this.setState({ status: 'empty' })
		}
	}


	render () {
		let { statusName, originator, shippingTypeName, receivingTypeName, outStockDate, signTime, sheetItems, sheetLostResults } = this.state.data || {};
		let { orderType } = this.sendData || {};
		let time = orderType == "S" ? outStockDate : signTime;
		return (
			<View style={styles.container}>
				<ScrollView style={{ flex: 1 }}>
					<View style={styles.itemBox}>
						<View style={[styles.itemBody]}>
							<View style={styles.detailsHead}>
								<View style={[commonStyle.row2, styles.headPd]}>
									<Text style={styles.detailsHeadTitle}>{orderType == "S" ? '发' : '收'}货单号 {this.sendData.sheetId || '-'}</Text>
									<Text style={styles.detailsHeadStatus}>{statusName || '-'}</Text>
								</View>
								<View style={[commonStyle.row2, styles.topp]}>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.value}>{(orderType == "S" ? shippingTypeName : receivingTypeName) || '-'}</Text>
										<Text style={orderStyle.xian}>|</Text>
										<Text style={orderStyle.value}>{time && time.substring(0, 10)} {orderType == "S" ? '发' : '收'}货</Text>
									</View>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>制单人:</Text>
										<Text style={orderStyle.value}>{originator || '-'}</Text>
									</View>
								</View>
							</View>
						</View>
					</View>

					<Text style={styles.title}>{orderType == "S" ? '发' : '收'}货信息</Text>
					<View style={styles.itemBox}>
						<View style={[styles.itemBody]}>

							{sheetItems && sheetItems.map((item, index) => {
								return (
									<View style={sheetItems.length - 1 != index ? [styles.mb, styles.bxian] : null} key={index}>
										<Text style={[orderStyle.value, styles.mb]}>{item.goodsName || '-'}</Text>
										<View style={[commonStyle.row3]}>
											<View style={{ flex: 1 }}>
												<Text style={[commen.lable, styles.pb]}>{orderType == "S" ? '发' : '收'}货量(吨)</Text>
												<Text style={orderStyle.value}>{(orderType == "S" ? item.sendQuantity : item.signQuantity) || '-'}</Text>
											</View>
											<View style={{ flex: 1 }}>
												<Text style={[commen.lable, styles.pb]}>结算量(吨)</Text>
												<Text style={orderStyle.value}>{item.settlementQuantity || '-'}</Text>
											</View>
										</View>
									</View>
								)
							})}
						</View>
					</View>

					<Text style={styles.title}>途损信息</Text>
					<View style={styles.itemBox}>
						{sheetLostResults && sheetLostResults.length ? (
							<View style={[styles.itemBody]}>

								{
									sheetLostResults && sheetLostResults.map((item, index) => {
										return (
											<View style={styles.goodsItem} key={index}>
												<View style={styles.goodsTitleBox}>
													<Text style={styles.goodsTitle}>{item.goodsName || '-'}</Text>
												</View>
												<View style={styles.bstyle}>
													<View style={styles.goodsItem}>
														<View style={[commonStyle.row2, styles.mb]}>
															<Text style={orderStyle.detailsHeadTitle}>{item.lostId || '-'}</Text>
															<Text style={orderStyle.value}>{item.createTime || '-'}</Text>
														</View>
														<View style={[commonStyle.row3, styles.mb]}>
															<View style={[{ flex: 1 }]}>
																<Text style={[commen.lable, styles.pb]}>途损量(吨)</Text>
																<Text style={orderStyle.value}>{item.lostTotalQuantity || '-'}</Text>
															</View>
															<View style={[{ flex: 1 }]}>
																<Text style={[commen.lable, styles.pb]}>途损类型</Text>
																<Text style={orderStyle.value}>{item.lostTypeName || '-'}</Text>
															</View>
															<View style={[{ flex: 1 }]}>
																<Text style={[commen.lable, styles.pb]}>交货方式</Text>
																<Text style={orderStyle.value}>{item.deliveryTypeName || '-'}</Text>
															</View>
														</View>
														<View style={[commonStyle.row3, styles.mb]}>
															<View style={[{ flex: 1 }]}>
																<Text style={[commen.lable, styles.pb]}>途损原因</Text>
																<Text style={orderStyle.value}>{item.lostTypeName || '-'}</Text>
															</View>
															<View style={[{ flex: 1 }]}>
																<Text style={[commen.lable, styles.pb]}>订单含税单价(元)</Text>
																<Text style={orderStyle.value}>{item.procurementPrice || '-'}</Text>
															</View>
															<View style={[{ flex: 1 }]}>
																<Text style={[commen.lable, styles.pb]}>总途损金额(元)</Text>
																<Text style={orderStyle.value}>{item.lostTotalAmt || '-'}</Text>
															</View>
														</View>
														<View style={[commonStyle.row3, styles.pb]}>
															<Text style={orderStyle.value}>承运商承担</Text>
															<Text style={orderStyle.xian}>|</Text>
															<Text style={orderStyle.value}>{item.forwarderAssumeQuantity || '-'}吨</Text>
															<Text style={orderStyle.xian}>|</Text>
															<Text style={orderStyle.value}>{item.forwarderAssumeQuantityAmt || '-'}元</Text>
														</View>
														<View style={commonStyle.row3}>
															<Text style={orderStyle.value}>我方承担</Text>
															<Text style={orderStyle.xian}>|</Text>
															<Text style={orderStyle.value}>{item.myAssumeQuantity || '-'}吨</Text>
															<Text style={orderStyle.xian}>|</Text>
															<Text style={orderStyle.value}>{item.myAssumeQuantityAmt || '-'}元</Text>
														</View>
													</View>
												</View>
											</View>
										)
									})
								}

							</View>
						) : (<View style={[styles.itemBody]}><Text>暂无数据</Text></View>)}

					</View>

				</ScrollView >
			</View >
		)
	}
}


const styles = StyleSheet.create({
	container: {
		flex: 1,
		backgroundColor: '#F5F5F5'
	},
	detailsHead: {
		//...commen.edge
	},
	title: {
		...commen.edge,
		fontSize: 14 * scale,
		color: style.color.font1
	},
	detailsHeadTitle: {
		fontSize: 14 * scale,
		color: style.color.font1
	},
	detailsHeadText: {
		fontSize: 14 * scale,
		color: style.color.font1
	},
	detailsHeadStatus: {
		fontSize: 14 * scale,
		color: style.color.font1
	},
	topp: {
		marginBottom: 10 * scale
	},

	headPd: {
		paddingTop: 8 * scale,
		paddingBottom: 15 * scale,
	},
	itemBox: {
		...commen.edge,
		marginBottom: 20 * scale
	},
	itemBody: {
		backgroundColor: style.color.white,
		borderRadius: 4 * scale,
		paddingTop: 15 * scale,
		paddingBottom: 15 * scale,
		marginTop: 5 * scale,
		...commen.edge
	},
	bao: {
		paddingBottom: 15 * scale,
		marginBottom: 10 * scale,
		borderBottomWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderBottomColor: '#E3E7F0',
	},
	pb: {
		paddingBottom: 5 * scale
	},
	mb: {
		marginBottom: 15 * scale
	},
	bxian: {
		borderBottomWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderBottomColor: '#E3E7F0',
		paddingBottom: 10 * scale
	},
	goodsTitleBox: {
		borderBottomWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderBottomColor: '#E3E7F0',
		position: 'relative',
		flexDirection: 'row',
		justifyContent: 'center',
		marginBottom: 15 * scale
	},
	goodsTitle: {
		position: 'absolute',
		top: -7 * scale,
		fontSize: 14 * scale
	},
	bstyle: {
		// borderBottomWidth: 1 / PixelRatio.get(),
		// borderStyle: 'solid',
		// borderBottomColor: '#E3E7F0',
		//paddingBottom: 15 * scale,
		marginBottom: 15 * scale
	},
	goodsItem: {
		marginTop: 10 * scale
	}
})